package com.wisdytech.linkdcs.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.system.dao.ISysDictIdxDao;
import com.wisdytech.linkdcs.system.model.SysDictIdx;
import com.wisdytech.linkdcs.system.service.ISystemDictIndexService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 
 * <p>
 * ClassName: SystemDictIndexServiceImpl
 * </p>
 * <p>
 * Description: 字典索引service层业务实现
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-13
 * </p>
 */
@Service("systemDictIndexService")
@Transactional
public class SystemDictIndexServiceImpl extends BaseService<ISysDictIdxDao, SysDictIdx> implements ISystemDictIndexService {

    @Resource
    private ISysDictIdxDao sysDictIdxDao;
    
    
    @Override
    public StandardResult addSysDictIndex(SysDictIdx sysDictIndex) {
        sysDictIndex.setDelFlag(CommonConstants.DEL_FLAG);
        sysDictIndex.setCreateDate(new Date());
        sysDictIndex.setCreateUser("test1");
        sysDictIndex.setPkId(BaseUtil.getUUID());
        //字典类型--不清楚这个0代表什么
        sysDictIndex.setDictType("0");
        int count;
        count = sysDictIdxDao.insert(sysDictIndex);
        if (count <= 0) {
            throw new ServiceException("新增失败");
        }
          return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateSysDictIndex(SysDictIdx sysDictIndex) {
        int count;
        count = sysDictIdxDao.updateById(sysDictIndex);
        if (count <= 0) {
            throw new ServiceException("修改失败");
        }
          return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult delSysDictIndex(String id) {
        int count;
        count = sysDictIdxDao.deleteById(id);
        if (count <= 0) {
            throw new ServiceException("删除失败");
        }
          return StandardResult.getSuccessResult();
    }

    
    @Override
    public PageInfo<SysDictIdx> listSysDictIdxs(String dictName, Filter filter) {
        
        EntityWrapper<SysDictIdx> ew =new EntityWrapper<>();
        if (!StringUtil.isEmpty(dictName)) {
            ew.like("dict_name", dictName);
        }
        List<SysDictIdx> lists =
                super.beforeList(ISysDictIdxDao.class, filter).selectList(ew.eq("del_flag", CommonConstants.DEL_FLAG));
        PageInfo<SysDictIdx> dictIdxPage=new PageInfo<>(lists);
        return dictIdxPage;
    }
    
    public SysDictIdx getSysDictIdx(String id ) {
        return sysDictIdxDao.selectById(id);
    }

}
